﻿/**
* @author Alexander Rindo
* @project Flashview
* @version 0.4
* @purpose Class for the managing toolbar buttons
**/

package com.alexrindo.flashview.UI
{
	import caurina.transitions.*;
	import caurina.transitions.properties.DisplayShortcuts;
	import com.alexrindo.flashview.data.GalleryData;
	import com.alexrindo.flashview.image.ImageManager;
	import com.alexrindo.flashview.UI.information.InformationBar;
	import com.alexrindo.flashview.UI.share.ShareToolbar;
	import com.alexrindo.flashview.tooltip.Tooltip;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.display.StageDisplayState;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class ButtonManager
	{
		private static var Instance:ButtonManager
		private static var Initializing:Boolean
		
		private var _images:ImageManager
		private var _data:GalleryData
		private var _tooltip:Tooltip
		private var _shareToolbar:ShareToolbar
		private var _infoBar:InformationBar
		
		public static const INFO_BUTTON:String = "infoIcon"
		public static const SAVE_BUTTON:String = "saveIcon"
		public static const SHARE_BUTTON:String = "shareIcon"
		public static const NEXT_ARROW:String = "next_arrrow"
		public static const PREV_ARROW:String = "prev_arrrow"
		public static const FULLSCREEN_BUTTON:String = "fullscreen_button"
		public static const PLAY_PAUSE_BUTTON:String = "play_pause_button"
		public static const FULLSCREEN_NEXT_ARROW:String = "fullscreen_next_arrrow"
		public static const FULLSCREEN_PREV_ARROW:String = "fullscreen_prev_arrrow"
		public static const EXIT_BUTTON:String = "exit_button"
		
		private var _buttonContainer:Array = new Array()
		private var _shareBarActive:Boolean
		private var _informationBarActive:Boolean
		
		private var _iconMenuItems:Array
		private var _arrowMenuItems:Array
		
		private var _stageObject:Stage
		
		public function ButtonManager() 
		{
			if (!Initializing)
			{
				throw new Error("This class is contructed as a singleton, please use the getInstance() method")
			}
			init()
		}
		
		private function init():void
		{
			_images = ImageManager.getInstance()
			_data = GalleryData.getInstance()
			_tooltip = new Tooltip()
		
			setupButtonCollections()
			DisplayShortcuts.init();
		}
		
		private function setupButtonCollections():void
		{
			_iconMenuItems = new Array(INFO_BUTTON, SAVE_BUTTON, SHARE_BUTTON)
			_arrowMenuItems = new Array(NEXT_ARROW,PREV_ARROW,FULLSCREEN_NEXT_ARROW,FULLSCREEN_PREV_ARROW)
		}
		
		public static function getInstance():ButtonManager
		{
			if (Instance == null)
			{
				Initializing = true
				Instance = new ButtonManager()
				Initializing = false
			}
			return Instance
		}
		
		public function setStageObject(stageObject:Stage):void
		{
			_stageObject = stageObject
		}
		
		public function addButton(object:DisplayObject, type:String):void
		{
			_buttonContainer[type] = object
		}
		
		public function addListeners():void
		{
			for (var i:int = 0; i < _iconMenuItems.length; i++) 
			{
				_buttonContainer[_iconMenuItems[i]].addEventListener(MouseEvent.ROLL_OVER, leftToolbarItem_over)
				_buttonContainer[_iconMenuItems[i]].addEventListener(MouseEvent.ROLL_OUT, leftToolbarItem_out)
			}
			
			for (var j:int = 0; j < _arrowMenuItems.length; j++) 
			{
				_buttonContainer[_arrowMenuItems[j]].addEventListener(MouseEvent.CLICK, arrow_click)
				_buttonContainer[_arrowMenuItems[j]].addEventListener(MouseEvent.ROLL_OVER, arrow_over)
				_buttonContainer[_arrowMenuItems[j]].addEventListener(MouseEvent.ROLL_OUT, arrow_out)
			}
			
			_buttonContainer[INFO_BUTTON].addEventListener(MouseEvent.CLICK, info_click)
			_buttonContainer[SAVE_BUTTON].addEventListener(MouseEvent.CLICK, save_click)
			_buttonContainer[SHARE_BUTTON].addEventListener(MouseEvent.CLICK, share_click)
			_buttonContainer[FULLSCREEN_BUTTON].addEventListener(MouseEvent.CLICK, fullscreen_click)
			_buttonContainer[PLAY_PAUSE_BUTTON].addEventListener(MouseEvent.ROLL_OVER, play_pause_over)
			_buttonContainer[PLAY_PAUSE_BUTTON].addEventListener(MouseEvent.ROLL_OUT, play_pause_out)
			_buttonContainer[PLAY_PAUSE_BUTTON].addEventListener(MouseEvent.CLICK, play_pause_click)
			_buttonContainer[EXIT_BUTTON].addEventListener(MouseEvent.ROLL_OVER, exit_over)
			_buttonContainer[EXIT_BUTTON].addEventListener(MouseEvent.ROLL_OUT, exit_out)
			_buttonContainer[EXIT_BUTTON].addEventListener(MouseEvent.CLICK, exit_click)
		}
		
		private function leftToolbarItem_over(e:MouseEvent):void
		{
			e.target.play()
			switch(e.target.name)
			{
				case SAVE_BUTTON:
				_tooltip.gotoAndStop(1)
				break
				
				case SHARE_BUTTON:
				_tooltip.gotoAndStop(2)
				break
				
				case INFO_BUTTON:
				_tooltip.gotoAndStop(3)
				break
			}
			
			_tooltip.x = -20 
			_tooltip.y = -40
			e.target.addChild(_tooltip)
		}
		
		private function leftToolbarItem_out(e:MouseEvent):void
		{
			if (_tooltip.parent != null)
			{
			e.target.removeChild(_tooltip)
			}
		}
		
		public function share_click(e:MouseEvent = null):void 
		{
			if (_informationBarActive)
			{
				_infoBar.visible = false
				_informationBarActive = false
			}
			
			if (!_shareBarActive)
			{
				removeToolTip()
				if (_shareToolbar == null)
				{
					_shareToolbar = new ShareToolbar()
					_shareToolbar.x = 51
					_shareToolbar.y = -62
					_buttonContainer[SHARE_BUTTON].parent.addChild(_shareToolbar)
				} else {
					_shareToolbar.visible = true
				}
			} else {
				_shareToolbar.visible = false
				addToolTip()
			}
			_shareBarActive = !_shareBarActive
		}
		
		public function info_click(e:MouseEvent = null):void 
		{
			if (_shareBarActive)
			{
				_shareToolbar.visible = false
				_shareBarActive = false
			}
			
			if (!_informationBarActive)
			{
				removeToolTip()
				if (_infoBar == null)
				{
					_infoBar = new InformationBar()
					_infoBar.x = 2.5
					_infoBar.y = -401
					_buttonContainer[INFO_BUTTON].parent.addChild(_infoBar)
				} else {
					_infoBar.visible = true
				}
			} else {
				_infoBar.visible = false
				addToolTip()
			}
			_informationBarActive = !_informationBarActive
		}
		
		private function addToolTip():void
		{
			for (var i:int = 0; i < _iconMenuItems.length; i++) 
			{
				_buttonContainer[_iconMenuItems[i]].addEventListener(MouseEvent.ROLL_OVER, leftToolbarItem_over)
				_buttonContainer[_iconMenuItems[i]].addEventListener(MouseEvent.ROLL_OVER, leftToolbarItem_out)
			}
		}
		
		private function removeToolTip():void
		{
			for (var i:int = 0; i < _iconMenuItems.length; i++) 
			{
				if (_buttonContainer[_iconMenuItems[i]].hasEventListener(MouseEvent.ROLL_OVER))
				{
					_buttonContainer[_iconMenuItems[i]].addEventListener(MouseEvent.ROLL_OVER, leftToolbarItem_over)
					_buttonContainer[_iconMenuItems[i]].addEventListener(MouseEvent.ROLL_OVER, leftToolbarItem_out)
				}
			}
			
			if (_tooltip.parent != null)
			{
			_tooltip.parent.removeChild(_tooltip)
			}
		}
		
		private function save_click(e:MouseEvent):void 
		{
			if (_data.protectImages == false)
			{
			_images.saveImage()
			}
		}
		
		
		//Arrow Listeners
		
		private function arrow_click(e:MouseEvent):void
		{
			e.currentTarget.innerArrow.play()
			
			switch(e.currentTarget)
			{
				case _buttonContainer[FULLSCREEN_PREV_ARROW]:
				_images.checkSlideShow(false)
				
				case _buttonContainer[PREV_ARROW]:
				_images.prevImage()
				break
				
				case _buttonContainer[FULLSCREEN_NEXT_ARROW]:
				_images.checkSlideShow(false)
				
				case _buttonContainer[NEXT_ARROW]:
				_images.nextImage()
				break
			}
		}
		
		private function arrow_over(e:MouseEvent):void
		{
			e.currentTarget.gotoAndPlay(1)
		}
		
		private function arrow_out(e:MouseEvent):void
		{
			e.currentTarget.gotoAndStop(1)
		}
		
		
		//Fullscreen listeners
		
		private function fullscreen_click(e:MouseEvent):void 
		{
			toggleFullScreen()
		}
		
		private function play_pause_out (e:MouseEvent):void 
		{
			Tweener.addTween (e.currentTarget, { _color: 0xFFFFFF, transition:"linear", time: 0.2 } )
		}
		private function play_pause_over (e:MouseEvent):void 
		{ 
			Tweener.addTween (e.currentTarget, { _color: 0x999999, transition:"linear", time: 0.2 } );
		}
		
		private function play_pause_click(e:MouseEvent):void 
		{
			_images.checkSlideShow(true)
		}
		
		private function exit_over(e:MouseEvent)
		{
			e.currentTarget.play()
		}
		
		private function exit_out(e:MouseEvent)
		{
			e.currentTarget.gotoAndStop(1)
		}
		
		private function exit_click(e:MouseEvent):void 
		{
			toggleFullScreen()
		}
		
		private function toggleFullScreen()
		{
			switch (_stageObject.displayState)
			{
				case StageDisplayState.NORMAL:
					_stageObject.displayState = StageDisplayState.FULL_SCREEN
					break;
					
				case StageDisplayState.FULL_SCREEN:
					_stageObject.displayState = StageDisplayState.NORMAL
					break;
			}
		}
		
		//Getters
		
		public function get infoButton():MovieClip
		{
			return _buttonContainer[INFO_BUTTON]
		}
		
		public function get shareButton():MovieClip
		{
			return _buttonContainer[SHARE_BUTTON]
		}
		
		public function get saveButton():MovieClip
		{
			return _buttonContainer[SAVE_BUTTON]
		}
		
		public function get playPauseButton():MovieClip
		{
			return _buttonContainer[PLAY_PAUSE_BUTTON]
		}
		
	}

}